<?php
defined('IN_TS') or die('Access Denied.');
switch($ts){

	//确认领取
	case "attend":

		$js = intval($_GET['js']);

    	$userid = intval($TS_USER['userid']);

		if($userid == 0) {
			getJson('请登录后再操作！',1);
		}

		$wishid = intval($_POST['wishid']);
		$memberid = intval($_POST['memberid']);
		$strWish = $new['wish']->find('wish',array(
			'wishid'=>$wishid,
		));

		if($userid<>$strWish['userid']&&$GLOBALS['TS_USER']['isadmin']==0){
			getJson('您没有操作权限！',1);
		}

		$userNum = $new['wish']->findCount('wish_users',array(
			'wishid'=>$wishid,
			'userid'=>$memberid,
		));

		if($userNum==0){
			getJson('此人不在认领名单中',1);
		}


		//标记他完成了
		$new['wish']->update('wish_users',array(
			'wishid'=>$wishid,
			'userid'=>$memberid,
		),array(
			'status'=>2,
			'uptime'=>time()
		));

		// 对积分进行处理
		aac('user') -> doScore('wish', 'do', 'attend',$memberid);


		//统计一下达成的
		$sql = "select sum(quota) as sums from ts_wish_users where wishid=".$wishid.' and status=2';
		$arr=$db->once_fetch_assoc($sql);
		$userDoNum=$arr['sums'];

		$new['wish']->update('wish',array(
			'wishid'=>$wishid,
		),array(
			'count_userdo'=>$userDoNum
		));

		//发一条广播

		$arrUser = aac('user')->find('user_info',array('userid'=>$strWish['userid']));

		$content = '帮助'.$arrUser['username'].'达成了心愿《<a href="'.tsUrl('wish','show',array('id'=>$wishid)).'">'.$strWish['title'].'</a>》！';
		aac('weibo')->create('weibo',array(
			'userid'=>$memberid,
			'content'=>$content,
			'isaudit'=>0,
			'addtime'=>date('Y-m-d H:i:s'),
			'uptime'=>date('Y-m-d H:i:s'),
		));

		//发送消息给买家
		$msg_userid = '0';
		$msg_touserid = $memberid;
		$msg_content = '你认领捐赠的《'.$strWish['title'].'》已收到，捐赠证书已经生成，请点击后面的链接访问，可以下载图片转发到你的朋友圈。';
		$msg_tourl = tsUrl('wish','cert',array('id'=>$wishid));
		aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content,$msg_tourl);

		//发送消息给发起人
		$msg_userid = '0';
		$msg_touserid = $userid;
		$msg_content = '你发起的心愿《'.$strWish['title'].'》已收货，请跟进心愿图书的后期处理，包括：在书的扉页上书写捐赠人姓名；录入图书时在备注中记录捐赠人姓名；拍照发给捐赠人。';
		$msg_tourl = tsUrl('wish','show',array('id'=>$wishid));
		aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content,$msg_tourl);




		echo '1';

		break;

	//确认没领取
	case "absent":

		$js = intval($_GET['js']);
		$userid = intval($TS_USER['userid']);

		if($userid == 0) {
			getJson('请登录后再操作！',1);
		}

		$wishid = intval($_POST['wishid']);
		$memberid = intval($_POST['memberid']);
		$strWish = $new['wish']->find('wish',array(
			'wishid'=>$wishid,
		));

		if($userid<>$strWish['userid']&&$GLOBALS['TS_USER']['isadmin']==0){
			getJson('您没有操作权限！',1);
		}

		$userNum = $new['wish']->findCount('wish_users',array(
			'wishid'=>$wishid,
			'userid'=>$memberid,
		));

		if($userNum==0){
			getJson('此人不在申领名单中',1);
		}


		//标记他没取
		$new['wish']->update('wish_users',array(
			'wishid'=>$wishid,
			'userid'=>$memberid,
		),array(
			'status'=>0,
			'uptime'=>time()
		));

		// 对积分进行处理
		aac('user') -> doScore($TS_URL['app'], $TS_URL['ac'], $TS_URL['ts'],$memberid);

		/* 对服务时长进行处理
		$strWish = $new['wish']->find('wish',array(
			'wishid'=>$wishid,
		));
		$strType = $new['wish']->find('wish_type',array(
			'typeid'=>$strWish['typeid'],
		));
		if($strType['isduty'] == 1){
			$term = intval((strtotime($strWish['endtime'])-strtotime($strWish['starttime']))/(1000*3600));
			$arrUser=$new['user']->find('user_info',array('userid'=>$memberid));
			$count_duty=$arrUser['count_duty']*1+$term*1;
			$new['user']->update('user_info',array(
				'userid'=>$memberid,
			),array(
				'count_duty'=>$count_duty,
			));
			aac('user') -> doScore('user', 'duty', 'verify',$memberid);
		}else{


		}
*/


		//统计一下达成的
		$sql = "select sum(quota) as sums from ts_wish_users where wishid=".$wishid.' and status=1';
		$arr=$db->once_fetch_assoc($sql);
		$userDoNum=$arr['sums'];

		$new['wish']->update('wish',array(
			'wishid'=>$wishid,
		),array(
			'count_userdo'=>$userDoNum
		));


		//wish
		$strWish = $new['wish']->find('wish',array(
			'wishid'=>$wishid,
		));

		echo '1';

		break;

	//取消申领心愿
	case "cancel":

		$js = intval($_GET['js']);

		$userid = intval($TS_USER['userid']);
		
		if($userid == 0) {
			echo 0;exit;
		}
		
		$wishid = intval($_POST['wishid']);
		
		$new['wish']->delete('wish_users',array(
		
			'wishid'=>$wishid,
			'userid'=>$userid,
		
		));

		//统计一下认领的
		$sql = "select sum(quota) as sums from ts_wish_users where wishid=".$wishid." and status>-1";
		$arr=$db->once_fetch_assoc($sql);
		$userWishNum=$arr['sums'];
		


		$new['wish']->update('wish',array(
			'wishid'=>$wishid,
		),array(
			'count_userwish'=>$userWishNum,
		));
		
		echo '1';
		
		break;
		
	//申领心愿
	case "join":
	
		$js = intval($_GET['js']);
		
	
		$userid = intval($TS_USER['userid']);
		
		if($userid == 0) {
			getJson('请登录后再认领心愿',$js);
		}
		
		$wishid = intval($_POST['wishid']);

        $strWish = $new['wish']->find('wish',array(
            'wishid'=>$wishid,
        ));

        if($strWish==''){
            getJson('不存在该心愿！',$js);
        }

		if($strWish['limittime']<=time()){
			getJson('已过认领期限',$js);
		}



		if($strWish['quota']>0&&$strWish['quota']<=$strWish['count_userwish']){
			getJson('心愿已领完',$js);
		}


		$username = trim($_POST['username']);
		$phone = trim($_POST['phone']);
		$quota = trim($_POST['quota']);

		if($username=='' || $phone=='' || $quota==''){
			getJson('姓名、手机号、认领数不能为空',$js);
		}


		if($quota>($strWish['quota']-$strWish['count_userwish'])){
			getJson('认领数超出了已认领余额',$js);
		}


		//删除之前的申领信息
		$new['wish']->delete('wish_users',array(
			'wishid'=>$wishid,
			'userid'=>$userid,
		));
		
		
		$new['wish']->create('wish_users',array(
			'wishid'=>$wishid,
			'userid'=>$userid,
			'quota'=>$quota,
			'limittime'=>strtotime("+2 day"),
			'status'=>0,
			'addtime'=>time(),
			'uptime'=>time()
		));

		//更新用户信息
		$new['wish']-> update('user_info',array(
			'userid'=>$userid,
		),array(
			'username'=>$username,
			'phone'=>$phone,
		));
/*
		if($iszhengshu==1){
			$new['wish']->create('wish_comment',array('wishid'=>$wishid,'userid'=>$userid,'content'=>"需要捐赠证书",'addtime'=>time()));
		}
*/

		$content = '认领了心愿《<a href="'.tsUrl('wish','show',array('id'=>$wishid)).'">'.$strWish['title'].'</a>》！';
		aac('weibo')->create('weibo',array(
			'userid'=>$userid,
			'content'=>$content,
			'isaudit'=>0,
			'addtime'=>date('Y-m-d H:i:s'),
			'uptime'=>date('Y-m-d H:i:s'),
		));

		//统计一下申领的
		$sql = "select sum(quota) as sums from ts_wish_users where wishid=".$wishid.' and status>-1';
		$arr=$db->once_fetch_assoc($sql);
		$userWishNum=$arr['sums'];
		

		$new['wish']->update('wish',array(
			'wishid'=>$wishid,
		),array(
			'count_userwish'=>$userWishNum
		));


		if($strWish['userid'] != $userid){
			$msg_userid = '0';
			$msg_touserid = $strWish['userid'];
			$msg_content = '你发布的心愿《'.$strWish['title'].'》有人认领了，快去看看吧^_^ ';
			$msg_tourl = tsUrl('wish','show',array('id'=>$wishid));
			aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content,$msg_tourl);
		}

		header('location: '.$_SERVER['HTTP_REFERER']);

		break;

	//确认发货
	case "send":

		$js = intval($_GET['js']);

		$userid = intval($TS_USER['userid']);

		if($userid == 0) {
			getJson('请登录后再操作！',1);
		}

		$wishid = intval($_POST['wishid']);
		$strWish = $new['wish']->find('wish',array(
			'wishid'=>$wishid,
		));
		$wishuser = $new['wish']->find('wish_users',array(
			'wishid'=>$wishid,'userid'=>$userid
		));

		if(!$wishuser){
			getJson('您没有操作权限！',1);
		}


		//标记他完成了
		$new['wish']->update('wish_users',array(
			'wishid'=>$wishid,
			'userid'=>$userid,
		),array(
			'status'=>1,
			'uptime'=>time()
		));

		//发广播
		$content = '认领的'.'心愿《<a href="'.tsUrl('wish','show',array('id'=>$wishid)).'">'.$strWish['title'].'</a>》已发货！';
		aac('weibo')->create('weibo',array(
			'userid'=>$userid,
			'content'=>$content,
			'isaudit'=>0,
			'addtime'=>date('Y-m-d H:i:s'),
			'uptime'=>date('Y-m-d H:i:s'),
		));

		if($strWish['userid'] != $userid){
			$msg_userid = '0';
			$msg_touserid = $strWish['userid'];
			$msg_content = $TS_USER['username'].'认领的《'.$strWish['title'].'》已经发货了，快去看看吧^_^ ';
			$msg_tourl = tsUrl('wish','show',array('id'=>$wishid));
			aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content,$msg_tourl);
		}



		echo '1';

		break;

	//否认收货
	case "notsend":

		$js = intval($_GET['js']);

		$userid = intval($TS_USER['userid']);

		if($userid == 0) {
			getJson('请登录后再操作！',1);
		}

		$wishid = intval($_POST['wishid']);
		$strWish = $new['wish']->find('wish',array(
			'wishid'=>$wishid,
		));
		$wishuser = $new['wish']->find('wish_users',array(
			'wishid'=>$wishid,'userid'=>$userid
		));

		if(!$wishuser){
			getJson('您没有操作权限！',1);
		}


		//标记没有收货
		$new['wish']->update('wish_users',array(
			'wishid'=>$wishid,
			'userid'=>$userid,
		),array(
			'status'=>0,
			'uptime'=>time()
		));


		echo '1';

		break;

	case 'deluser':
		$wishid = intval($_GET['wishid']);
		$userid = intval($_GET['userid']);
		$strWish = $new['wish']->find('wish',array(
			'wishid'=>$wishid,
		));
		if($strWish['userid']!=$userid && $GLOBALS['TS_USER']['isadmin']==0){
			tsNotice('非法操作！');
		}
		$new['wish']->update('wish_users',array(
			'wishid'=>$wishid,
			'userid'=>$userid,
		),array(
			'status'=>-1,
			'uptime'=>time()
		));

		echo '1';

		break;

	// 上传封面
    case "photo":
		$photo_id = mt_rand(100000, 999999);
		$dir = "wish";
        $arrUpload = tsUpload($_FILES['file'], $photo_id, $dir, array('jpg','gif','png', 'jpeg'));

        if($arrUpload){
			// 删除缓存缩略图	
            tsDimg($arrUpload['url'], $dir,'48','48',$arrUpload['path']);
            tsDimg($arrUpload['url'], $dir,'16','16',$arrUpload['path']);
            tsDimg($arrUpload['url'], $dir,'32','32',$arrUpload['path']);
            tsDimg($arrUpload['url'], $dir,'24','24',$arrUpload['path']);
            tsDimg($arrUpload['url'], $div,'120','120',$arrUpload['path']);

			echo $arrUpload["url"];
        }else{
			echo 0;
        }

        break;

}
